﻿using Entity;
using IRealize;
using Microsoft.Data.SqlClient;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Realize
{
    public class SG:ISG
    {
        

        public async Task<List<Salary_Grant>> Server(string sqlserver)
        {
            Task<List<Salary_Grant>> task = Task<List<Salary_Grant>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from salary_grant where check_status = 0";
                    return con.Query<Salary_Grant>(sql).ToList();
                }
            });
            return await task;
        }
        public async Task<List<Salary_Grant>> Server3(string sqlserver)
        {
            Task<List<Salary_Grant>> task = Task<List<Salary_Grant>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from salary_grant";
                    return con.Query<Salary_Grant>(sql).ToList();
                }
            });
            return await task;
        }
        public async Task<List<Salary_Grant>> Server2(string sg_id, string sqlserver)
        {
            Task<List<Salary_Grant>> task = Task<List<Salary_Grant>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from salary_grant where salary_grant_id ='{sg_id}'";
                    return con.Query<Salary_Grant>(sql).ToList();
                }
            });
            return await task;
        }

        public async Task<int> Insert(string sg_id, string ss_id, string fk_id, string fk_name, string sk_id, string sk_name, string tk_id, string tk_name, int ha, double ss_sum, double sp_sum, string register, DateTime register_time, string sqlserver)
        {
            using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
            {
                string sql = $@"insert into salary_grant(salary_grant_id,salary_standard_id,first_kind_id,first_kind_name,second_kind_id,second_kind_name,third_kind_id,
                                third_kind_name,human_amount,salary_standard_sum,salary_paid_sum,register,regist_time)
                 values('{sg_id}','{ss_id}','{fk_id}','{fk_name}','{sk_id}','{sk_name}','{tk_id}','{tk_name}',{ha},{ss_sum},{sp_sum},'{register}','{register_time}')";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        public async Task<int> Update(string sg_id, decimal sp_sum, string fhr, DateTime fhsj, string sqlserver)
        {
            using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
            {
                string sql = $@"update salary_grant set check_status = 1 , salary_paid_sum={sp_sum},checker = '{fhr}',check_time = '{fhsj}' 
                  where salary_grant_id = '{sg_id}'";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }
    }
}
